package leetcode;

import java.util.Arrays;

/**
 * 描述:209. 长度最小的子数组
 *
 * @author Madison You
 * @created 23:57
 */
public class Mid_209_MinSubArrayLen {
    public static void main(String[] args) {
        int[] ints = {2,3,1,2,4,3};
        new Mid_209_MinSubArrayLen().minSubArrayLen(7, ints);
    }

    public int minSubArrayLen(int target, int[] nums) {
        /*先设定一个静止指针left*/
        int left = 0;
        int sum = 0;
        int len = Integer.MAX_VALUE;
        for (int right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= target) {
                len = Math.min(len, right - left + 1);
                sum -= nums[left++];
            }
        }
        return len == Integer.MAX_VALUE ? 0 : len;
    }

}
